<template>
  <scroll class="listview" :data="data" ref="listview" @scroll="scroll" :listenScroll="listenScroll" :probeType="probeType">
    <ul>
      <li v-for="(group,index) in data" :key="index" class="list-group" ref="listGroup">
        <h2 class="list-group-title">{{ group.title }}</h2>
        <ul>
          <li v-for="(item, index2) in group.items" :key="index2" class="list-group-item" @click="selectItem(item)">
            <img width="50" height="50" v-lazy="item.avatar" class="avatar">
            <span class="name">{{ item.name }}</span>
          </li>
        </ul>
      </li>
    </ul>
    <div class="list-shortcut" @touchstart.stop.prevent='onShortcutTouchStart' @touchmove='onShortcutTouchMove'>
      <ul>
        <li v-for="(item, index) in shortcutList" :key="index" class="item" :data-index='index' :class="{'current': currentIndex===index}">
          {{ item }}
        </li>
      </ul>
    </div>
    <div class="list-fixed" v-show="fixedTitle" ref="fixed">
      <div class="fixed-title">{{ fixedTitle }}</div>
    </div>
    <div class="loading-container" v-show="!data.length">
      <loading></loading>
    </div>
  </scroll>
</template>

<script>
import scroll from 'base/scroll/scroll'
import { getData } from 'common/js/dom'
import loading from 'base/loading/loading'

const ANCHOR_HEIGHT = 18
const TITLE_HEIGHT = 30

export default {
  data() {
    return {
      listenScroll: true,
      scrollY: -1,
      currentIndex: 0,
      probeType: 3,
      heighList: [],
      diff: -1
    }
  },
  props: {
    data: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    // 存储侧边栏的字母
    shortcutList() {
      let temp = this.data.map((group) => {
        return group.title.substr(0, 1)
      })
      return temp
    },
    fixedTitle() {
      if(this.scrollY > 0) {
        return ''
      }
      return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
    }
  },
  components: {
    scroll,
    loading
  },
  created() {
    // 为什么不在data数据里面直接创建，因为不需要监听数据的变化，不用添加get和set方法
    this.touch = {}
  },
  methods: {
    refresh() {
      this.$refs.listview.refresh()
    },
    selectItem(item) {
      this.$emit('select', item)
    },
    // scroll组件滚动触发的事件 pos: scroll组件传回来的位置
    scroll(pos) {
      this.scrollY = pos.y
    },
    // 当触摸侧边栏触发的事件
    onShortcutTouchStart(e) {
      let anchorIndex = getData(e.target, 'index')
      // 拿到刚开始的触点
      let firsttouch = e.touches[0]
      this.touch.y1 = firsttouch.pageY
      this.touch.anchorIndex = anchorIndex
      this._scrollTo(anchorIndex)
    },
    // 当滑动侧边栏促发的事件
    onShortcutTouchMove(e) {
      let endtouch = e.touches[0]
      this.touch.y2 = endtouch.pageY
      let delta = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
      let anchorIndex = parseInt(this.touch.anchorIndex) + delta
      this._scrollTo(anchorIndex)
    },
    _scrollTo(anchorIndex) {
      // 点击侧边栏空白区域
      if (!anchorIndex && anchorIndex !== 0) {
        return
      }
      this.scrollY = -this.heighList[anchorIndex]
      this.$refs.listview.scrollToElement(this.$refs.listGroup[anchorIndex], 0)
    },
    // 计算列表高度
    _calculateHeight() {
      let list = this.$refs.listGroup
      let height = 0
      this.heighList.push(0)
      list.forEach(item => {
        height += item.clientHeight
        this.heighList.push(height)
      })
    }
  },
  watch: {
    // 监听data数据的变化，当数据变化需要计算高度列表
    data() {
      // 数据改变反应到dom的变化需要20ms的更新时间
      setTimeout(() => {
        this._calculateHeight()
      }, 20)
    },
    // 监听scrollY数据的变化，当改变的时候，计算currentIndex
    scrollY(newY) {
      // 当滚动到顶部之前
      if (newY > 0) {
        this.currentIndex = 0
      }
      // 当滚动到中间部分
      newY = -newY
      for (let i = 0; i < this.heighList.length - 1; i++) {
        let prev = this.heighList[i]
        let next = this.heighList[i + 1]
        if (!next || (newY >= prev && newY < next)) {
          this.currentIndex = i
          this.diff = next - newY
          return
        }
      }
      // 当滚动到最下面的下面
      // 实际上不能滚动到最下面的下面，即不可能滚动到9010
    },
    diff(newVal) {
      let fixedTop = (newVal > 0 && newVal <= TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
      if(this.fixedTop === fixedTop) {
        return
      }
      this.fixedTop = fixedTop
      this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~common/stylus/variable';

.listview
  position: relative
  top: 0
  bottom: 0
  left: 0
  width: 100%
  height: 100%
  overflow: hidden
  .list-group
    padding-bottom: 30px
    .list-group-title
      height: 30px
      line-height: 30px
      padding-left: 20px
      font-size: $font-size-small
      color: $color-text-l
      background: $color-highlight-background
    .list-group-item
      display: flex
      align-items: center
      padding: 20px 0 0 30px
      .avatar
        border-radius: 50%
      .name
        margin-left: 20px
        color: $color-text-l
        font-size: $font-size-medium
  .list-shortcut
    position: absolute
    right: 0
    top: 50%
    transform: translateY(-50%)
    z-index: 30
    border-radius: 10px
    width: 20px
    padding: 20px 0
    background: rgba(0, 0, 0, 0.3)
    font-family: Helvetica
    text-align: center
    .item
      padding: 3px
      line-height: 1
      color: $color-text-l
      font-size: 12px
      &.current
        color: $color-theme
  .list-fixed
    position: absolute
    top: 0
    left: 0
    width: 100%
    .fixed-title
      height: 30px
      line-height: 30px
      padding-left: 20px
      font-size: $font-size-small
      color: $color-text-l
      background: $color-highlight-background
  .loading-container
    position: absolute
    width: 100%
    top: 50%
    transform: translateY(-50%)
</style>
